package com.zhu.user.util;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;

/**
 * Created by DELL on 2016/1/5.
 */
@Service
public class PasswordHelper {

    private RandomNumberGenerator numberGenerator = new SecureRandomNumberGenerator();

    private String algorithmName = "md5";

    private int hashIterations = 2;

    public void setNumberGenerator(RandomNumberGenerator numberGenerator) {
        this.numberGenerator = numberGenerator;
    }

    public void setAlgorithmName(String algorithmName) {
        this.algorithmName = algorithmName;
    }

    public void setHashIterations(int hashIterations) {
        this.hashIterations = hashIterations;
    }

    public String encryptPassword(String pass, ByteSource salt){
        return new SimpleHash(algorithmName, pass, salt, hashIterations).toHex();
    }

    public String generateSalt(){
        return numberGenerator.nextBytes().toHex();
    }
}
